import * as types from '../mutation-types'
import _ from 'lodash'
const state = {
  device: {
    isMobile: false,
    isTablet: false
  },
  sidebar: {
    opened: false,
    hidden: false
  },
  effect: {
    translate3d: true
  },
  personal: {
    edit: false,
    info: {
      name: '',
      message: '',
      gender: 0,
      birthday: ''
    },
    user: {},
    account: {
      loginName: '未登录',
      status: '',
      token: {
      }
    }
  },
  projects: []
}

const mutations = {
  [types.TOGGLE_DEVICE] (state, device) {
    state.device.isMobile = device === 'mobile'
    state.device.isTablet = device === 'tablet'
  },

  [types.TOGGLE_SIDEBAR] (state, config) {
    if (state.device.isMobile && config.hasOwnProperty('opened')) {
      state.sidebar.opened = config.opened
    } else {
      state.sidebar.opened = true
    }

    if (config.hasOwnProperty('hidden')) {
      state.sidebar.hidden = config.hidden
    }
  },

  [types.SWITCH_EFFECT] (state, effectItem) {
    for (let name in effectItem) {
      state.effect[name] = effectItem[name]
    }
  },

  [types.PERSONAL_INFO_TOGGLE_EDIT] (state) {
    state.personal.edit = !state.personal.edit
  },

  [types.PERSONAL_INFO_EDIT] (state, info) {
    state.personal.info = info
  },

  [types.UPDATE] (state, payload) {
    state.personal.info[payload.key] = payload.value
  },

  [types.SET_PROJECTS] (state, payload) {
    state.projects = payload
  },

  [types.GET_PROJECTS_BY_ID] (state, articleId) {
    _.filter(state.projects, o => {
      return o.articleId === articleId
    })
  },

  [types.SET_USER] (state, payload) {
    state.personal.user = payload
  },

  [types.SET_USER_INFO] (state, payload) {
    state.personal.info = payload
  },

  [types.LOGIN] (state, payload) {
    state.personal.account.token = payload.token
    state.personal.account.loginName = payload.loginName
    state.personal.account.status = payload.status
  },
  [types.REMOVE_PROJECTS] (state, articleId) {
    state.projects.splice(state.projects.findIndex(item => item.articleId === articleId), 1)
    window.sessionStorage.setItem('projects', JSON.stringify(state.projects))
  },
  [types.ADD_ARTICLE] (state, playload) {
    state.projects.push(playload)
    window.sessionStorage.setItem('projects', JSON.stringify(state.projects))
  },
  [types.ALTER_ARTICLE] (state, playload) {
    const index = _.findIndex(state.projects, (o) => { return o.articleId === playload.articleId })
    console.log(playload.articleId)
    console.log(index)
    console.log(state.projects[index])
    state.projects[index][playload.key] = playload.value
    window.sessionStorage.setItem('projects', JSON.stringify(state.projects))
  },
  [types.ISLOGIN] (state) {
    if (window.sessionStorage.getItem('personal.account.status')) {
      state.personal.user = JSON.parse(window.sessionStorage.getItem('personal.account.user'))
      state.personal.account.token = JSON.parse(window.sessionStorage.getItem('personal.account.token'))
      state.personal.account.loginName = window.sessionStorage.getItem('personal.account.loginName')
      state.personal.account.status = window.sessionStorage.getItem('personal.account.status')
      state.personal.info = JSON.parse(window.sessionStorage.getItem('personal.info.userinfo'))
      state.personal.projects = JSON.parse(window.sessionStorage.getItem('projects'))
      state.projects = JSON.parse(window.sessionStorage.getItem('projects'))
    }
  }
}

export default {
  state,
  mutations
}
